package com.blog.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blog.dao.TestDAO;
import com.blog.domain.IbatisTest;
import com.blog.service.TestService;

@Controller
public class TestController {

	@Autowired
	TestDAO service;

	@RequestMapping("/test")
	public String test(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		IbatisTest dto = new IbatisTest();
		dto.setmId("id");
		dto.setmPwd("pwd123");
		dto.setmCode(service.selectMCodeMax());

		service.insertAll(dto);

		List<String> list = service.selectAll();
		model.addAttribute("model", list);
		return "body/index";
	}

	@RequestMapping("/test2")
	public String test2(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		IbatisTest dto = new IbatisTest();
		dto.setmId("id");
		dto.setmPwd("pwd123");
		dto.setmCode(service.selectMCodeMax());

		service.insertAll(dto);

		List<String> list = service.selectAll();
		model.addAttribute("model", list);
		return "body/index";
	}

	@RequestMapping("/error")
	public String test4(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		return "error";
	}

	@RequestMapping("/index")
	public String test5(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		return "body/index";
	}

	@RequestMapping("/comment")
	public String comment(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		return "comment";
	}

	// Jquery
	// @Controller에서 @ResponseBody로 지정시 반환값에 한글이 있으면 깨지는 현상을 방지하기 위해 사용한다.
	@RequestMapping("jqueryTest")
	public @ResponseBody
	Map getJqueryTestMsg(HttpServletRequest request) {
		System.out.println(1231);
		String msg = "";

		if ("".equals(request.getParameter("id"))
				|| request.getParameter("id") == null) {
			msg = "헐?";
		} else if ("".equals(request.getParameter("pwd"))
				|| request.getParameter("pwd") == null) {
			msg = "3";
		} else {
			String id = request.getParameter("id");
			String pwd = request.getParameter("pwd");

			if ("testid".equals(id) && "testpwd".equals(pwd)) {
				msg = "1";
			} else {
				msg = "2";
			}
		}
		Map<String, Comparable> map = new HashMap();
		map.put("msg", msg);
		map.put("ret", true);
		return map;
	}

}
